public class RepetitionZero {
    public void duplicateZeros(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                int j = arr.length - 1;
                while (j > i + 1) {
                    arr[j] = arr[j - 1];
                    j--;
                }
                if (i + 1 < arr.length) {
                    arr[i + 1] = 0;
                }
                i++;
            }
        }
    }

    public void duplicateZeros2(int[] arr) {
        int i = 0;
        int j = -1;
        int len = arr.length;
        for (; i < len; i++) {
            if (arr[i] != 0) {
                j++;
            } else {
                j += 2;
            }
            if (j >= len - 1) {
                break;
            }
        }

        if (j >= len) {
            i--;
            arr[--j] = 0;
            j--;
        }

        for (; i >= 0 && j >= 0; i--) {
            if (arr[i] != 0) {
                arr[j--] = arr[i];
            } else {
                arr[j--] = arr[i];
                if (j < 0) {
                    break;
                }
                arr[j--] = arr[i];
            }
        }
    }
}
